# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def __init__(self):
        self.tree = TreeNode()

    def constructMaximumBinaryTree(self, nums):
        """
        :type nums: List[int]
        :rtype: Optional[TreeNode]
        """
        self.loop(nums, self.tree)
        return self.tree

    def loop(self, nums, current_tree):
        root_num = max(nums)
        index = nums.index(root_num)
        current_tree.val = root_num

        left_nums = nums[:index]
        right_nums = nums[index + 1:]

        if left_nums:
            current_tree.left = TreeNode()
            self.loop(left_nums, current_tree.left)
        if right_nums:
            current_tree.right = TreeNode()
            self.loop(right_nums, current_tree.right)
